(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.feeType.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'feeTypeService', 'storageService'];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, feeTypeService, storageService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {},siteCode = storageService.getUserInfo().siteCode;;
		var log = $scope.loggerFactory.getLogger("feeTypeCreate");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.feeType = {};
			feeTypeService.initGeneral($scope, ctrl);
			authService.create(params).then(function(response) {
				ctrl.feeType = response.data || params;
				ctrl.feeType.siteCode = siteCode;
			});
		};
		ctrl.feeTypeNameChanged = function () {
			if(ctrl.feeType.feeTypeName) {
				ctrl.feeType.feeType = siteCode + "-" + ctrl.feeType.feeTypeName;
			} else {
				ctrl.feeType.feeType = '';
			}
		}
		ctrl.createSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.feeType.taskId) {
				return authService.createContinueSave(ctrl.feeType).then(saveSuccessful, saveFailed);
			} else {
				log.debug('prop in feeType:')
				for (var name in ctrl.feeType) {
					log.debug(name + '=' + ctrl.feeType[name]);
				}
				return authService.createSave(ctrl.feeType).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.feeType.taskId) {
				return authService.createContinueSubmit(ctrl.feeType).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.feeType).then(submitSuccessful, submitFailed);
			}
		};

		$scope.$on("COUNTRYCODE_OK", function(event, param) {
			var returnEntities = param.returnEntities;
			if(angular.isArray(returnEntities) && returnEntities[0] && returnEntities[0].countryCode != ctrl.feeType.oldCountryCode) {
				ctrl.feeType.oldCountryCode = returnEntities[0].countryCode;
				ctrl.feeType.stateCode = '';
			}
			event.preventDefault();
			event.stopPropagation();
		});


		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.feeType.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.feetype.list',
					'state' : 'cda.feeType.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);